package member1;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.JLayeredPane;

@SuppressWarnings("serial")
public class Input extends JPanel { // JPanel을 상속했으므로, Input 패널,  is a 관계
	
	private JLabel lblNewLabel; // has a 관계
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JTextField username;
	private JTextField address;
	private JTextField phone;
	private JTextField email;
	private JTextField age;
	private JButton confirm;
	
	Data6[] data; 
	private JLabel datalabel;

	/**
	 * Create the panel.
	 */
	public Input() {
		setBackground(Color.PINK);
		setLayout(null);
		add(getLblNewLabel());
		add(getLabel());
		add(getLabel_1());
		add(getLabel_2());
		add(getLabel_3());
		add(getUsername());
		add(getAddress());
		add(getPhone());
		add(getEmail());
		add(getAge());
		add(getConfirm());
		add(getDatalabel());

	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("성명");
			lblNewLabel.setFont(new Font("굴림", Font.PLAIN, 11));
			lblNewLabel.setBounds(146, 56, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("주소");
			label.setFont(new Font("굴림", Font.PLAIN, 11));
			label.setBounds(146, 93, 57, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("연락처");
			label_1.setFont(new Font("굴림", Font.PLAIN, 11));
			label_1.setBounds(146, 130, 57, 15);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("이메일");
			label_2.setFont(new Font("굴림", Font.PLAIN, 11));
			label_2.setBounds(146, 167, 57, 15);
		}
		return label_2;
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("나이");
			label_3.setFont(new Font("굴림", Font.PLAIN, 11));
			label_3.setBounds(146, 204, 57, 15);
		}
		return label_3;
	}
	private JTextField getUsername() {
		if (username == null) {
			username = new JTextField();
			username.setBounds(206, 51, 116, 21);
			username.setColumns(10);
		}
		
		
		
		return username;
	}
	private JTextField getAddress() {
		if (address == null) {
			address = new JTextField();
			address.setColumns(10);
			address.setBounds(206, 89, 116, 21);
		}
		return address;
	}
	private JTextField getPhone() {
		if (phone == null) {
			phone = new JTextField();
			phone.setColumns(10);
			phone.setBounds(206, 127, 116, 21);
		}
		return phone;
	}
	private JTextField getEmail() {
		if (email == null) {
			email = new JTextField();
			email.setColumns(10);
			email.setBounds(206, 165, 116, 21);
		}
		return email;
	}
	private JTextField getAge() {
		if (age == null) {
			age = new JTextField();
			age.setColumns(10);
			age.setBounds(206, 203, 116, 21);
		}
		return age;
	}
	private JButton getConfirm() {
		if (confirm == null) {
			confirm = new JButton("확인");
			confirm.setFont(new Font("굴림", Font.PLAIN, 11));
			confirm.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					// 데이터 입력 여부 검사 
					boolean flag = true; // 모든 값을 입력했을때 
					if(username.getText().equals("")) // 공백이라면 
						flag = false; 
					else if (address.getText().equals(""))
						flag = false;
					else if (phone.getText().equals(""))
						flag = false;
					else if (email.getText().equals(""))
						flag = false;
					else if (age.getText().equals(""))
						flag = false;
					
					if (flag){
						
						Data6 d = new Data6(); 
						d.name = username.getText();
						d.address = address.getText();
						d.phone = phone.getText();
						d.email = email.getText();
						
						char[] number = age.getText().toCharArray(); 
						
						if (!(number[0] >= 48 && number[0] <= 57))
						{
							datalabel.setText("나이는 숫자를 입력해야 합니다");
							age.setText("");
							
						}
						else{
							
							d.age = Integer.parseInt(age.getText());
							
							for (int i = 0;i<data.length;i++){
								if (data[i] == null){
									data[i] = d; // 데이터가 입력된 시점
									
									username.setText("");
									address.setText("");
									phone.setText("");
									email.setText("");
									age.setText("");
									
									username.requestFocus(); // 커서 이동 
									datalabel.setText("데이터가 저장되었습니다.");
									
									break;
								}
							}
						}			
					}
					
				}
			});
			confirm.setBounds(225, 244, 97, 23);
		}
		return confirm;
	}
	
	public void setD (Data6[] data){
		this.data = data;  // 필드에서 선언된 data에 데이터를 저장해주는 작업 
	}
	private JLabel getDatalabel() {
		if (datalabel == null) {
			datalabel = new JLabel("   데이터 입력  페이지");
			datalabel.setOpaque(true);
			datalabel.setForeground(Color.WHITE);
			datalabel.setFont(new Font("나눔손글씨 펜", Font.PLAIN, 25));
			datalabel.setBackground(Color.DARK_GRAY);
			datalabel.setBounds(146, 10, 196, 21);
		}
		return datalabel;
	}
}
